import minimist from "minimist"
import { resolve, dirname } from 'path'
import { fileURLToPath } from "url";
import esbuild from "esbuild";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const args = minimist(process.argv.slice(2))

const target = args._[0] || 'reactivity'
const format = args.f || 'life';

const entry = resolve(__dirname, `../packages/${target}/src/index.ts`)
// 解析 package.json 
let pkg;
await import(`../packages/${target}/package.json`, { assert: { type: 'json' } }).then(module =>
    pkg = module.default
)

esbuild.context({
    entryPoints: [entry],
    outfile: resolve(__dirname, `../packages/${target}/dist/${target}.${format}.js`),
    bundle: true,
    platform: 'browser',
    sourcemap: true,
    format: format,
    globalName: pkg.buildOptions?.name,
}).then((ctx) => {
    console.log("start dev")

    return ctx.watch() // 监听文件变化
})